Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

Encapsulation in java

Access modifiers

In encapsulation the concept of access modifiers is very important and below are the explanation when and where the access modifiers to be used. The three access modifiers are Public,Private and Protected Public : The public modifier is the most permissive. A member (class, method, or field) that is declared public is accessible from anywhere. It can be accessed from objects, external packages, and any derived classes. Private : The private modifier is the most restrictive. The methods or data members declared as private are accessible only within the class in which they are declared. Any other class of the same package will not be able to access these members. Private means “only visible within the enclosing class”. Protected : The protected modifier is specified using the keyword protected. The methods or data members declared as protected are accessible within the same package or subclasses in different packages. Protected means “only visible within the enclosing class and any subclasses”. Default When no access modifier is specified for a class, method, or data member, it is said to be having the default access modifier. The data members, classes, or methods that are not declared using any access modifiers i.e., having default access modifiers are accessible only within the same package. These access modifiers contribute towards establishing the principle of data encapsulation in Object-Oriented Programming (OOP), where the data is bundled with the methods that operate on that data. They help in maintaining the integrity of the code, making the software design more robust and secure.

  📌TAGS

★Class ★ Method ★ Object ★ java ★ oops ★Encapsulation ★ Access modifiers

Tutorials